package com.example.wlzz.controller;
import com.example.wlzz.dto.UserInfoDTO;
import com.example.wlzz.entity.School;
import com.example.wlzz.enums.ResultEnum;
import com.example.wlzz.form.CrawlerForm;
import com.example.wlzz.repository.SchoolRepository;
import com.example.wlzz.service.UserInfoService;
import com.example.wlzz.utils.ResultVOUtil;
import com.example.wlzz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;

@RestController
@RequestMapping("/wlzzNewCrawler")
@Slf4j
@Api(tags = "New爬虫模块（扩展）【这里您只需填写好个人资料的学校名称就ok，其他服务器会帮您选择】")
public class AutoCrawlerController {
    @Autowired
    private CrawlerController crawlerController;
    @Autowired
    private MongoliaCrawlerController mongoliaCrawlerController;
    @Autowired
    private NanjingController nanjingController;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SchoolRepository schoolRepository;
    @PostMapping("/first")
    @ApiOperation("第一步：进入这里获取登陆的cookie和验证码")
    public ResultVO first(@ApiParam("token") @RequestParam("token") String token){
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(school.getId().equals(1)){
           return crawlerController.first(token);
        }else if(school.getId().equals(2)){
           return mongoliaCrawlerController.first(token);
        }else if(school.getId().equals(3)){
            return nanjingController.first(token);
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }
    }
    @PostMapping("/second")
    @ApiOperation("第二步：将【第一步】拿到的cookie放进对应的输入框里(登陆成功则返回对应的cookie)" +
                             "<br>【提示：登陆成功将会更新个人信息的学号和密码】")
    public ResultVO second(@Valid CrawlerForm crawlerForm , BindingResult bindingResult,
                           @ApiParam("token") @RequestParam("token") String token)throws Exception{
        log.warn("登录信息,crawlerForm={}",crawlerForm);
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(bindingResult.hasErrors()) {
            return ResultVOUtil.error(ResultEnum.LOGIN_SCHOOL_ERROR.getCode(),ResultEnum.LOGIN_SCHOOL_ERROR.getMessage());
        }
        if(school.getId().equals(1)){
            return crawlerController.second(crawlerForm,bindingResult,token);
        }else if(school.getId().equals(2)){
            return mongoliaCrawlerController.second(crawlerForm,bindingResult,token);
        }else if(school.getId().equals(3)){
            return nanjingController.second(crawlerForm,bindingResult,token);
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }
    }
    @PostMapping("/getNowClass")
    @ApiOperation("获得当前学期课表")
    public ResultVO getNowClass(@ApiParam("token") @RequestParam("token") String token,
                                @RequestParam("cookie") String cookie)throws Exception {
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(school.getId().equals(1)){
            return crawlerController.getDateClass(token,cookie,"default");
        }else if(school.getId().equals(2)){
            return mongoliaCrawlerController.getNowClass(token,cookie);
        }else if(school.getId().equals(3)){
            return nanjingController.getNowClass(token,cookie);
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }
    }
    @PostMapping("/getDepartment")
    @ApiOperation("获得院系")
    public ResultVO getDepartment(@ApiParam("token") @RequestParam("token") String token){
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(school.getId().equals(1)){
            return crawlerController.getDepartment(token);
        }else if(school.getId().equals(2)){
            return mongoliaCrawlerController.getDepartment(token);
        }else if(school.getId().equals(3)){
            return nanjingController.getDepartment(token);
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }
    }

    @PostMapping("/getDepartmentForMajor")
    @ApiOperation("根据院系选择专业")
    public ResultVO getDepartmentForMajor(@ApiParam("token") @RequestParam("token") String token,
                                        @RequestParam("cookie") String cookie,
                                        @ApiParam("上课院系") @RequestParam(value = "yuanxi") String yuanxi,
                                        @ApiParam("上课年级") @RequestParam(value = "nj") String nj){
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(school.getId().equals(1)){
            return crawlerController.getDepartmentForMajor(token,cookie,yuanxi,nj);
        }else if(school.getId().equals(2)){
            return mongoliaCrawlerController.getDepartmentForMajor(token,cookie,yuanxi,nj);
        }else if(school.getId().equals(3)){
            return nanjingController.getDepartmentForMajor(token,cookie,yuanxi,nj);
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }

    }

    @PostMapping("/getMajorKlass")
    @ApiOperation("根据专业搜索课程表")
    public ResultVO getMajorKlass(@ApiParam("token") @RequestParam("token") String token,
                                  @RequestParam("cookie") String cookie,
                                  @ApiParam("学期") @RequestParam("xueqi") String xueqi,
                                  @ApiParam("院系") @RequestParam("yuanxi") String yuanxi,
                                  @ApiParam("专业") @RequestParam("zhuanye") String zhuanye,
                                  @ApiParam("年级") @RequestParam("nianji") String nianji){
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(school.getId().equals(1)){
            return crawlerController.getMajorClass(token,cookie,xueqi,yuanxi,zhuanye,nianji);
        }else if(school.getId().equals(2)){
            return mongoliaCrawlerController.getMajorKlass(token,cookie,xueqi,yuanxi,zhuanye,nianji);
        }else if(school.getId().equals(3)){
            return ResultVOUtil.error(ResultEnum.NAN_SCHOOL.getCode(),ResultEnum.NAN_SCHOOL.getMessage());
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }
    }


    @PostMapping("/getGrade")
    @ApiOperation("根据学期获取成绩")
    public ResultVO getGrade(@ApiParam("token") @RequestParam("token") String token,
                             @ApiParam("日期-例子:<br>2017-2018-2<br>2017-2018-1<br>2018-2019-1") @RequestParam(value = "xnxqh",defaultValue = "") String xnxqh,
                             @RequestParam("cookie") String cookie
    ) {
        log.warn("学期日期, data={}",xnxqh);
        UserInfoDTO userInfo = (UserInfoDTO)userInfoService.getUserInfo(token).getData();
        School school =  schoolRepository.findBySchoolName(userInfo.getSchoolName());
        if(school==null){
            return ResultVOUtil.error(ResultEnum.SCHOOL_NAME_NXIST.getCode(),ResultEnum.SCHOOL_NAME_NXIST.getMessage());
        }
        if(school.getId().equals(1)){
            return crawlerController.getGrade(token, xnxqh, cookie);
        }else if(school.getId().equals(2)){
            return mongoliaCrawlerController.getGrade(token, xnxqh, cookie);
        }else if(school.getId().equals(3)){
            return nanjingController.getGrade(token, xnxqh, cookie);
        }else{
            return ResultVOUtil.error(ResultEnum.SCHOOL_NO_EXIST.getCode(),ResultEnum.SCHOOL_NO_EXIST.getMessage());
        }
    }
}
